package com.tjmc.nsia.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.nsia.core.dao.entity.NsiaChat;
import com.tjmc.nsia.core.dao.mapper.NsiaChatMapper;
import com.tjmc.nsia.core.domain.PageRespDto;
import com.tjmc.nsia.core.dto.req.ChatBody;
import com.tjmc.nsia.core.service.NsiaChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024/06/22
 */
@Service
public class NsiaChatServiceImpl extends ServiceImpl<NsiaChatMapper, NsiaChat> implements NsiaChatService {

    @Autowired
    private NsiaChatMapper nsiaChatMapper;

    @Override
    public PageRespDto<NsiaChat> listChats(ChatBody chatBody) {
        Page<NsiaChat> page = new Page<>();
        page.setCurrent(chatBody.getPageNum());
        page.setSize(chatBody.getPageSize());
        QueryWrapper<NsiaChat> queryWrapper = null;
        if(!Objects.isNull(chatBody.getSessionId()) && chatBody.getSessionId() > 0) {
            queryWrapper = new QueryWrapper<NsiaChat>().eq("session_id", chatBody.getSessionId());
        }
        Page<NsiaChat> nsiaChatPage = nsiaChatMapper.selectPage(page, queryWrapper);
        return PageRespDto.of(chatBody.getPageNum(), chatBody.getPageSize(), nsiaChatMapper.selectCount(queryWrapper), nsiaChatPage.getRecords());
    }

    @Override
    public void deleteChats(Integer sessionId) {
        if(!Objects.isNull(sessionId) && sessionId > 0) {
            nsiaChatMapper.delete(new QueryWrapper<NsiaChat>().eq("session_id", sessionId));
        }
    }

    @Override
    public List<NsiaChat> getChats(Integer sessionId) {
        if(!Objects.isNull(sessionId) && sessionId > 0) {
            nsiaChatMapper.selectList(new QueryWrapper<NsiaChat>().eq("session_id", sessionId));
        }
        return null;
    }


}
